import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {getList,getClassify,getArticle} from '../service/index'
// import {ClassifyListItem, ListLeftItem} from '../utils/knowledge'
export interface IndexModelState {
  list:any;
  classifyList:any;
  articleList:any;
  leftDetail:any;
}

export interface IndexModelType {
  namespace: 'knowledge';
  state: IndexModelState;
  effects: {
    getList:Effect;
    getClassify:Effect;
    getArticle:Effect;
    getLeftDetail:Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
  };
//   subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'knowledge',

  state: {
    list:[],
    classifyList:[],
    articleList:[],
    leftDetail:[]
  },

  effects: {
    *getList({payload},{call,put}){
        let res=yield call(getList)
        yield put({
            type:'save',
            payload:{
                list:res.data
            }
        })
    },
    *getClassify({payload},{call,put}){
      let res=yield call(getClassify)
        yield put({
            type:'save',
            payload:{
                classifyList:res.data
            }
        })
    },
    *getArticle({payload},{call,put}){
      let res=yield call(getArticle)
        yield put({
            type:'save',
            payload:{
              articleList:res.data
            }
        })
    },
    *getLeftDetail({payload},{call,put}){
      let res=yield call(getArticle,payload.id)
        yield put({
            type:'save',
            payload:{
              leftDetail:res.data
            }
        })
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  }
//   subscriptions: {
//     setup({ dispatch, history }) {
//       return history.listen(({ pathname }) => {
//         if (pathname === '/') {
//           dispatch({
//             type: 'query',
//           });
//         }
//       });
//     },
//   },
};

export default IndexModel;